<?php

namespace App\Console\Commands;

use App\Helpers\SelfEncrypt;
use App\Models\ManageAdminModel;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class initAdmin extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'admin:init';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        DB::table('manage_admins')->truncate();
        $app_name = config('app.name','yhf');
        if(config('app.env') == 'local'){
            $admin_name = 'admin';
            $login_pwd = 'admin123';
            $safe_pwd = 'admin123';
        }else{
            $admin_name = SelfEncrypt::randStr(2,'abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ').SelfEncrypt::randStr(6);
            $login_pwd = SelfEncrypt::randStr(8);
            $safe_pwd = SelfEncrypt::randStr(8);
        }
        $login_code = SelfEncrypt::createSalt(8);
        $safe_code = SelfEncrypt::createSalt(8);
        ManageAdminModel::insert([
            'mgr_name'=>SelfEncrypt::encrypt($admin_name),
            'mgr_email'=>$app_name.'@gmail.com',
            'mgr_mobile'=>'18899998888',
            'mgr_token'=>SelfEncrypt::randStr(24),
            'parent_id'=>0,
            'login_cipher'=>SelfEncrypt::make($login_pwd.$login_code),
            'login_code'=>$login_code,
            'safe_cipher'=>SelfEncrypt::make($safe_pwd.$safe_code),
            'safe_code'=>$safe_code,
            'is_enable'=>1,
            'is_super'=>1,
            'created_at'=>time(),
            'created_ip'=>ip2long(request()->getClientIp())
        ]);
        $this->info('Admin:'.$admin_name);
        $this->info('login password:'.$login_pwd);
        $this->info('safe password:'.$safe_pwd);
    }
}
